﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;

namespace TARS.User
{
    public partial class NewUserManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                loaddata();
        }
        protected void loaddata()
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                var load = db.t_BranchesInfo;
                cbbBraches.DataSource = load.ToList();
                cbbBraches.DataTextField = "Branches_Name";
                cbbBraches.DataValueField = "ID";
                cbbBraches.DataBind();
            }
        }
        protected void SendMail()
        {
            string pass = txtLName.Text + DateTime.Now.ToString("MM/dd/yyyy");
            string user = txtSSN.Text;
            // Gmail Address from where you send the mail
            var fromAddress = "khanhquang14@gmail.com";
            // any address where the email will be sending
            var toAddress = txtEmail.Text.ToString();
            //Password of your gmail address
            const string fromPassword = "diemhong90";
            // Passing the values and make a email formate to display
            string subject = "User Passord";
            string body = "From: " + "khanhquang14@gmail.com" + "\n";
            body += "Email: " + txtEmail.Text + "\n";
            body += "Subject: " + "User Pass " + "\n";
            body += "Question: \n" + user +"\n"+ pass + "\n";
            // smtp settings
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                smtp.Timeout = 20000;
            }
            // Passing values to smtp object
            smtp.Send(fromAddress, toAddress, subject, body);
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
           
            
        }

        protected void btnRegist_Click1(object sender, EventArgs e)
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                SendMail();
                    
                    DateTime dob = Convert.ToDateTime(txtDOB.SelectedDate.ToString());
                    Guid braches = new Guid(cbbBraches.SelectedValue.ToString());
                    DateTime time = Convert.ToDateTime(DateTime.Now.ToString());
                    string pass = Encryption.GetMd5Hash(txtLName.Text + DateTime.Now.ToString("MM/dd/yyyy"));
                    string user = txtSSN.Text;
                
                    string type = cbbUsertype.SelectedValue.ToString();
                    var inserNewRegist = db.Insert_User(user, pass, braches, time, type);
                    var load = db.t_User.Where(x => x.Username == user);
                    foreach (var ss in load)
                    {
                        var insert = db.Insert_EmployeeInfo(txtFName.Text, txtFName.Text, txtAddress.Text, txtCity.Text,
                            txtState.Text, txtZipCode.Text, txtSSN.Text, dob, ss.ID, time);

                    }
                
            }
        }
    }
}